package edu.sysu.cshare.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import javax.swing.JButton;

public class Dlg_NewCourse extends JDialog {

	private BindingGroup m_bindingGroup;
	private JPanel m_contentPane;
	private edu.sysu.cshare.dao.Course course = new edu.sysu.cshare.dao.Course();
	private JTextField examDescJTextField;
	private JTextField nameJTextField;
	private JSlider pointsJSlider;
	private JTextField typeJTextField;
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Dlg_NewCourse dialog = new Dlg_NewCourse();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Dlg_NewCourse() {
		setBounds(100, 100, 450, 300);
		m_contentPane = new JPanel();
		setContentPane(m_contentPane);
		//
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, -17, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4 };
		m_contentPane.setLayout(gridBagLayout);

		JLabel examDescLabel = new JLabel("ExamDesc:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		m_contentPane.add(examDescLabel, labelGbc_0);

		examDescJTextField = new JTextField();
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		m_contentPane.add(examDescJTextField, componentGbc_0);

		JLabel nameLabel = new JLabel("Name:");
		GridBagConstraints labelGbc_1 = new GridBagConstraints();
		labelGbc_1.insets = new Insets(5, 5, 5, 5);
		labelGbc_1.gridx = 0;
		labelGbc_1.gridy = 1;
		m_contentPane.add(nameLabel, labelGbc_1);

		nameJTextField = new JTextField();
		GridBagConstraints componentGbc_1 = new GridBagConstraints();
		componentGbc_1.insets = new Insets(5, 0, 5, 0);
		componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_1.gridx = 1;
		componentGbc_1.gridy = 1;
		m_contentPane.add(nameJTextField, componentGbc_1);

		JLabel pointsLabel = new JLabel("Points:");
		GridBagConstraints labelGbc_2 = new GridBagConstraints();
		labelGbc_2.insets = new Insets(5, 5, 5, 5);
		labelGbc_2.gridx = 0;
		labelGbc_2.gridy = 2;
		m_contentPane.add(pointsLabel, labelGbc_2);

		pointsJSlider = new JSlider();
		pointsJSlider.setValue(2);
		pointsJSlider.setMinimum(1);
		pointsJSlider.setMaximum(10);
		GridBagConstraints componentGbc_2 = new GridBagConstraints();
		componentGbc_2.insets = new Insets(5, 0, 5, 0);
		componentGbc_2.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_2.gridx = 1;
		componentGbc_2.gridy = 2;
		m_contentPane.add(pointsJSlider, componentGbc_2);

		JLabel typeLabel = new JLabel("Type:");
		GridBagConstraints labelGbc_3 = new GridBagConstraints();
		labelGbc_3.insets = new Insets(5, 5, 5, 5);
		labelGbc_3.gridx = 0;
		labelGbc_3.gridy = 3;
		m_contentPane.add(typeLabel, labelGbc_3);

		typeJTextField = new JTextField();
		GridBagConstraints componentGbc_3 = new GridBagConstraints();
		componentGbc_3.insets = new Insets(5, 0, 5, 0);
		componentGbc_3.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_3.gridx = 1;
		componentGbc_3.gridy = 3;
		m_contentPane.add(typeJTextField, componentGbc_3);
		
		btnNewButton = new JButton("确定");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 7;
		m_contentPane.add(btnNewButton, gbc_btnNewButton);

		if (course != null) {
			m_bindingGroup = initDataBindings();
		}
	}

	protected BindingGroup initDataBindings() {
		BeanProperty<edu.sysu.cshare.dao.Course, java.lang.String> examDescProperty = BeanProperty
				.create("examDesc");
		BeanProperty<javax.swing.JTextField, java.lang.String> textProperty = BeanProperty
				.create("text");
		AutoBinding<edu.sysu.cshare.dao.Course, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, course,
						examDescProperty, examDescJTextField, textProperty);
		autoBinding.bind();
		//
		BeanProperty<edu.sysu.cshare.dao.Course, java.lang.String> nameProperty = BeanProperty
				.create("name");
		BeanProperty<javax.swing.JTextField, java.lang.String> textProperty_1 = BeanProperty
				.create("text");
		AutoBinding<edu.sysu.cshare.dao.Course, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding_1 = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, course,
						nameProperty, nameJTextField, textProperty_1);
		autoBinding_1.bind();
		//
		BeanProperty<edu.sysu.cshare.dao.Course, java.lang.Integer> pointsProperty = BeanProperty
				.create("points");
		BeanProperty<javax.swing.JSlider, java.lang.Integer> valueProperty = BeanProperty
				.create("value");
		AutoBinding<edu.sysu.cshare.dao.Course, java.lang.Integer, javax.swing.JSlider, java.lang.Integer> autoBinding_2 = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, course,
						pointsProperty, pointsJSlider, valueProperty);
		autoBinding_2.bind();
		//
		BeanProperty<edu.sysu.cshare.dao.Course, java.lang.String> typeProperty = BeanProperty
				.create("type");
		BeanProperty<javax.swing.JTextField, java.lang.String> textProperty_2 = BeanProperty
				.create("text");
		AutoBinding<edu.sysu.cshare.dao.Course, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding_3 = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, course,
						typeProperty, typeJTextField, textProperty_2);
		autoBinding_3.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_1);
		bindingGroup.addBinding(autoBinding_2);
		bindingGroup.addBinding(autoBinding_3);
		//
		return bindingGroup;
	}

	public edu.sysu.cshare.dao.Course getCourse() {
		return course;
	}

	public void setCourse(edu.sysu.cshare.dao.Course newCourse) {
		setCourse(newCourse, true);
	}

	public void setCourse(edu.sysu.cshare.dao.Course newCourse, boolean update) {
		course = newCourse;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (course != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}

}
